package com.example.gomi.intelligentcitypipe.utils;

import android.util.Log;

import com.example.gomi.intelligentcitypipe.DAO.PipeInfo;
import com.example.gomi.intelligentcitypipe.bean.Response;
import com.example.gomi.intelligentcitypipe.serial.PipeProtocol;

public class ResponseMessageUtils {

    public final static byte COMMAND_WRITE_PAG11 = 0x0b;

    public static void analyze(Response response, PipeInfo pipeInfo) {
        switch (response.getType()) {
            case PipeProtocol.COMMAND_BATTERY_LEVEL:

                pipeInfo.setBatteryLevel(response.getBody()[0]);
                break;
            case PipeProtocol.COMMAND_READ_PAGE3:
                int[] id = new int[5];
                long pipeid = 0;
                for (int i = 1; i <= 5; i++) {
                    id[i-1] = response.getBody()[i] & 0xff;
                    pipeid = pipeid * 256 + id[i-1];
                }
                pipeInfo.setPipeid(pipeid);
                Log.e("PipeProtocol", "get data mGetPage3: True. PIP INFO:"+pipeid);
                break;
            case PipeProtocol.COMMAND_READ_PAGE9:
                int year = (response.getBody()[0] & 0xff) * 256 + (response.getBody()[1] & 0xff);
                int month = response.getBody()[2] & 0xff;
                int day = response.getBody()[3] & 0xff;
                int hour = response.getBody()[4] & 0xff;
                if(year>1970&&year<2018){
                    pipeInfo.setYear(year);
                    pipeInfo.setMonth(month);
                    pipeInfo.setDay(day);
                    pipeInfo.setHour(hour);
                }else{
                    pipeInfo.setYear(2018);
                    pipeInfo.setMonth(07);
                    pipeInfo.setDay(05);
                    pipeInfo.setHour(04);
                }
                Log.e("MessageUtils","year:"+ year +" ,month：" + month);
                break;
            case PipeProtocol.COMMAND_WRITE_PAGE9:
                // we don't do it now
                break;
            case PipeProtocol.COMMAND_READ_PAG11:
                int minute = response.getBody()[1] & 0xff;
                int type = response.getBody()[2] & 0xff;
                int company = response.getBody()[3] & 0xff;
                int employee = response.getBody()[4] & 0xff;
                pipeInfo.setMinute(minute);
                pipeInfo.setType(type);
                pipeInfo.setCompany(company);
                pipeInfo.setJobid(employee);
                Log.e("MessageUtils","minute:"+minute+",employee："+employee);
                break;
            case COMMAND_WRITE_PAG11:
                // we don't do it now
                break;
            case PipeProtocol.COMMAND_HUMITURE_READ:
                int aa = (response.getBody()[0]<<8)+ (response.getBody()[1]);
                double temperature = -46.85 + (aa)*175.72/65536.0;
                pipeInfo.setTemperature(temperature);
                int bb = (response.getBody()[2]<<8)+ (response.getBody()[3]);
                double humidity = -6.0+(bb)*125.0/65536.0;
                pipeInfo.setHumidity(humidity);
                Log.e("MessageUtils","温度:"+temperature+",湿度："+humidity);
                break;
        }
    }
    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * 方法二：
     * byte[] to hex string
     *
     * @param bytes
     * @return
     */
    public static String bytesToHex(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int index = 0;
        for(byte b : bytes) { // 利用位运算进行转换，可以看作方法一的变种
            buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
            buf[index++] = HEX_CHAR[b & 0xf];
        }

        return new String(buf);
    }
}
